import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'
import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill'
import { NodeModulesPolyfillPlugin } from '@esbuild-plugins/node-modules-polyfill'
import { modifyVars } from './lessVars'

// const baseUrl = 'http://xapc.zhichu.tech/'
// const baseUrl = 'http://hu.zhichu.tech/'
// const baseUrl = 'https://case.wuhuzcw.cn/'
// const baseUrl = 'http://hlj.zhichu.tech/'
const baseUrl = 'http://121.229.202.28:9977/'
function resolve (p) {
  return path.resolve(__dirname, p)
}

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  optimizeDeps: {
    esbuildOptions: {
      // Node.js global to browser globalThis
      define: {
        global: 'globalThis'
      },
      // Enable esbuild polyfill plugins
      plugins: [
        NodeGlobalsPolyfillPlugin({
          buffer: true,
          process: true
        }),
        NodeModulesPolyfillPlugin()
      ]
    }
  },
  server: {
    port: 5175,
    proxy: {
      '/api/': {
        target: baseUrl,
        changeOrigin: true
        // rewrite: path => path.replace(/^\/api/, '')
      },
      '/upload/': {
        target: baseUrl,
        changeOrigin: true
        // rewrite: path => path.replace(/^\/api/, '')
      },
      '/uploads/': {
        target: baseUrl,
        changeOrigin: true
        // rewrite: path => path.replace(/^\/api/, '')
      }
    }
  },
  resolve: {
    alias: {
      '@src': resolve('src'),
      '@js': resolve('src/assets/js'),
      '@img': resolve('src/assets/img'),
      '@redux': resolve('src/redux'),
      '@router': resolve('src/router'),
      '@components': resolve('src/components'),
      '@pages': resolve('src/pages'),
      '@hooks': resolve('src/hooks')
    }
  },
  css: {
    preprocessorOptions: {
      less: {
        modifyVars,
        javascriptEnabled: true
      }
    }
  }
})
